import jsMind from 'jsmind'
import 'jsmind/style/jsmind.css'

window.jsMind = jsMind

const $ = jsMind.$

/* eslint-disable */
/**
 * Override edit_node_end jsmind.view_provider.js
 * @param view {jm.view}
 * @param callback {(function(topic, node):promise)}
 * @returns {function}
 */
export function override_edit_node_end (view, callback) {
  return function edit_node_end () {
    if (this.editing_node != null) {
      var node = this.editing_node;
      this.editing_node = null;
      var view_data = node._data.view;
      var element = view_data.element;
      var topic = this.e_editor.value;
      element.style.zIndex = 'auto';
      element.removeChild(this.e_editor);
      if (topic === '' || node.topic === topic) {
        if (this.opts.support_html) {
          $.h(element, node.topic);
        } else {
          $.t(element, node.topic);
        }
      } else {
        this.jm.update_node(node.id, topic);
      }
      if (topic !== '') {
        callback(topic, node).catch(() => {
          // 添加不成功移除节点
          this.jm.remove_node(node.id)
        })
      }
    }
    this.e_panel.focus();
  }.bind(view)
}

/**
 * Override handle_delnode jsmind.shortcut_provider.js
 * this.handles['delnode'] = this.handle_delnode;
 * @param shortcut {jm.shortcut}
 * @param callback {(function(node): promise)}
 * @returns {(function(*, *): void)|*}
 */
export function override_handle_delnode (shortcut, callback) {
  return function handle_delnode (_jm, e) {
    var selected_node = _jm.get_selected_node();
    if (!!selected_node && !selected_node.isroot) {
      callback(selected_node).then(() => {
        _jm.select_node(selected_node.parent);
        _jm.remove_node(selected_node);
      })
    }
  }.bind(shortcut)
}
/* eslint-enable */

export default jsMind
